<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="LeetCode,">










<meta name="description" content="A001 Two Sum原文链接  Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may">
<meta name="keywords" content="LeetCode">
<meta property="og:type" content="article">
<meta property="og:title" content="LeetCode 1-10">
<meta property="og:url" content="http://yoursite.com/2019/11/07/LeetCode-1-10/index.html">
<meta property="og:site_name" content="平凡小札">
<meta property="og:description" content="A001 Two Sum原文链接  Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-11-07T16:05:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="LeetCode 1-10">
<meta name="twitter:description" content="A001 Two Sum原文链接  Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/11/07/LeetCode-1-10/">





  <title>LeetCode 1-10 | 平凡小札</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <a href="https://github.com/SimFG/SimFG.github.io"><img style="position: absolute; top: 0; right: 0;" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_white_ffffff.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1"></a>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">平凡小札</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/11/07/LeetCode-1-10/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="影决">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="平凡小札">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">LeetCode 1-10</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-07T23:56:42+08:00">
                2019-11-07
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="A001-Two-Sum"><a href="#A001-Two-Sum" class="headerlink" title="A001 Two Sum"></a>A001 Two Sum</h2><p><a href="https://leetcode-cn.com/problems/two-sum/" target="_blank" rel="noopener">原文链接</a></p>
<blockquote>
<p>Given an array of integers, return indices of the two numbers such that they add up to a specific target.</p>
<p>You may assume that each input would have exactly one solution, and you may not use the same element twice.</p>
<p>Example:</p>
<p>Given nums = [2, 7, 11, 15], target = 9,</p>
<p>Because nums[0] + nums[1] = 2 + 7 = 9,</p>
<p>return [0, 1].</p>
</blockquote>
<p>分析： </p>
<pre><code>1. 声明一个HashMap（key为当前数字，value为当前数字在数组中的序号）
2. 循环数组，循环体如下：（当前数字使用cur表示）
    1. 查看target-cur是否存在与当前map的key中；存在则返回当前的序号和target-cur的序号
    2. 将当前数字添加字HashMap中
3. 返回[-1, -1]</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">int</span>[] twoSum(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target) &#123;</span><br><span class="line">    HashMap&lt;Integer, Integer&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    <span class="keyword">int</span> result[] = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;-<span class="number">1</span>, -<span class="number">1</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (nums == <span class="keyword">null</span> || nums.length &lt; <span class="number">2</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.length; i++)&#123;</span><br><span class="line">        <span class="keyword">int</span> j = target - nums[i];</span><br><span class="line">        <span class="keyword">if</span> (hashMap.containsKey(j))&#123;</span><br><span class="line">            result[<span class="number">0</span>] = hashMap.get(j);</span><br><span class="line">            result[<span class="number">1</span>] = i;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        hashMap.put(nums[i], i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="A002-Add-Two-Numbers"><a href="#A002-Add-Two-Numbers" class="headerlink" title="A002 Add Two Numbers"></a>A002 Add Two Numbers</h2><p><a href="https://leetcode-cn.com/problems/add-two-numbers/" target="_blank" rel="noopener">原文链接</a></p>
<blockquote>
<p>You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.</p>
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>
<p>Example:</p>
<p>Input: (2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)</p>
<p>Output: 7 -&gt; 0 -&gt; 8</p>
<p>Explanation: 342 + 465 = 807.</p>
</blockquote>
<p>分析：</p>
<pre><code>1. 声明变量，其中res表示结果变量，cur表示当前节点，a、b分别表示l1和l2的当前节点，sum表示当前的结果值
2. 循环l1和l2，控制条件为当前节点不为null
    1. 分别判断a和b节点是否为空，因为两个ListNode长度可能不一，如果不为空，将该节点的值加值sum上
    2. a和b当前节点完成后，生成cur的下一节点，这个节点的值为sum%10
    3. 切换cur的节点，然后对sum更新，便于下一个节点计算，注意：如果存在进位，也就是sum这时候就会大于10，sum/10之后就会等于1，这个进位会自动存储到下一次循环
3. 判断当前sum是否为1，如果为1，说明在进行最后一次相加时存在进位，那么这时候就需要新生成一个节点
4. 返回res.next，因为res的第一个值为0</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * public class ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode next;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x) &#123; val = x; &#125;</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> ListNode <span class="title">addTwoNumbers</span><span class="params">(ListNode l1, ListNode l2)</span> </span>&#123;</span><br><span class="line">    ListNode res = <span class="keyword">new</span> ListNode(<span class="number">0</span>);</span><br><span class="line">    ListNode cur = res;</span><br><span class="line">    ListNode a = l1, b = l2;</span><br><span class="line">    <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(a != <span class="keyword">null</span> || b != <span class="keyword">null</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(a != <span class="keyword">null</span>)&#123;</span><br><span class="line">            sum += a.val;</span><br><span class="line">            a = a.next;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span>(b != <span class="keyword">null</span>)&#123;</span><br><span class="line">            sum += b.val;</span><br><span class="line">            b = b.next;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        cur.next = <span class="keyword">new</span> ListNode(sum % <span class="number">10</span>);</span><br><span class="line">        cur = cur.next;</span><br><span class="line">        sum /= <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(sum == <span class="number">1</span>)&#123;</span><br><span class="line">        cur.next = <span class="keyword">new</span> ListNode(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res.next;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="A003-Longest-Substring-Without-Repeating-Characters"><a href="#A003-Longest-Substring-Without-Repeating-Characters" class="headerlink" title="A003 Longest Substring Without Repeating Characters"></a>A003 Longest Substring Without Repeating Characters</h2><p><a href="https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/" target="_blank" rel="noopener">原文链接</a></p>
<blockquote>
<p>Given a string, find the length of the longest substring without repeating characters.</p>
<p>Example 1:</p>
<p>Input: “abcabcbb”</p>
<p>Output: 3 </p>
<p>Explanation: The answer is “abc”, with the length of 3. </p>
<p>Example 2:</p>
<p>Input: “bbbbb”</p>
<p>Output: 1</p>
<p>Explanation: The answer is “b”, with the length of 1.</p>
<p>Example 3:</p>
<p>Input: “pwwkew”</p>
<p>Output: 3</p>
<p>Explanation: The answer is “wke”, with the length of 3. </p>
<p>Note that the answer must be a substring, “pwke” is a subsequence and not a substring.</p>
</blockquote>
<p>分析：</p>
<pre><code>1. 声明res，用于存储当前最大子串的长度，声明map，其中key为当前字符，value为当前字符的序号
2. 遍历当前字符串，其中j标识了当前子串中的第一个字符位置
    1. 查看map中key是否包含当前字符，如果包含更新j的值，j的值为map中value的值加一，也就是表示在当前子串中与当前字符一样的字符位置后一位
    2. 将当前字符和字符位置添加至map中
    3. 更新res
3. 返回res</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">lengthOfLongestSubstring</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (s == <span class="keyword">null</span> || s.length() == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    HashMap&lt;Character, Integer&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>, j = <span class="number">0</span>; i &lt; s.length(); i++)&#123;</span><br><span class="line">        <span class="keyword">if</span> (map.keySet().contains(s.charAt(i)))&#123;</span><br><span class="line">            j = Math.max(j, map.get(s.charAt(i)) + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        map.put(s.charAt(i), i);</span><br><span class="line">        res = Math.max(res, i - j + <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="A004-Median-of-Two-Sorted-Arrays"><a href="#A004-Median-of-Two-Sorted-Arrays" class="headerlink" title="A004 Median of Two Sorted Arrays"></a>A004 Median of Two Sorted Arrays</h2><p><a href="https://leetcode-cn.com/problems/median-of-two-sorted-arrays/" target="_blank" rel="noopener">原文链接</a></p>
<blockquote>
<p>There are two sorted arrays nums1 and nums2 of size m and n respectively.</p>
<p>Find the median of the two sorted arrays. <strong>The overall run time complexity should be O(log (m+n))</strong>.</p>
<p>You may assume nums1 and nums2 cannot be both empty.</p>
<p>Example 1:</p>
<p>nums1 = [1, 3]<br>nums2 = [2]</p>
<p>The median is 2.0</p>
<p>Example 2:</p>
<p>nums1 = [1, 2]<br>nums2 = [3, 4]</p>
<p>The median is (2 + 3)/2 = 2.5</p>
</blockquote>
<p>分析：</p>
<pre><code>1. 假设nums1的长度总是小于nums2的长度
2. 声明各种变量，其中cut1和cut2表示两个数组切分的位置，cutL和cutR表示第一个数组基于上一次切分位置当前切分位置左右的元素个数（详解见下）
     cut1 = 2, cutL = 3, cutR = 1
1 7 8 | 9 
2 3 | 6 10 13
   cut2 = 2 
3. 循环，条件为cut1小于等于数组nums1的长度
    1. 更新cut1，（二分法更新 cut1 = (cutR - cutL)/2 + cutL）
    2. 更新cut2，其中cut1 + cut2 = len/2 这是固定的，因为寻找中位数，那么这个数的左右两边的树的个数就是相同的
    3. 计算切分位置的左右值，分别记为L1/R1/L2/R2
        L1 = 8, R1 = 9
        1 7 8 | 9 
        2 3 | 6 10 13
        L2 = 3, R2 = 6
    4. 处理一种极端情况，那就是如果切分位置为最开始，那么L1或者L2的值就不存在，那么这时候就需要赋值为无穷小，
        因为这L和R的值只是用来做判断，且在取值时时取L1L2中最大R1R2中最小的，也就不会去到这个无穷的值了，自然如果切分位置为末尾，那么R1或R2的值为无穷大
    5. 如果L1大于R2了，说明第一个切分位置应该往左移，这时候L1就会变小，因为这两个数组为有序数组，自然这时候第二个切分位置就会往右移，
        R2就会变大，就会往L1小于R2的趋势发展，因为在理想切分位置下，应该满足L1 &lt; R2 &amp;&amp; L2 &lt; R1，这就说明在切分位置的左边元素都小于切分位置的右边元素，那么中位数就是在L1/L2/R1/R2四个数字中
        操作：更新cutR = cut1 - 1，那么下一次cut1的位置就由（cutL -- cut1 -- cutR）变为（cutL - cut1 - cutR --），其中‘-’表示元素，cutL/cut1/cutR均代表位置
     cutL cut1 cutR
        |---|---|
        |-|-|----
    cutL cut1 cutR
    6. 如果L2大于R1，那么第一个切分位置应该往右移，也就是cutL = cut1 + 1
    7. 如果达到理想情况，那么这时候获取中位数
        1. 如果数组总长度为偶数，那么去左边最大右边最小的值，然后除以2便是结果
        2. 如果是奇数的话，那么结果就是右边的最小值</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">findMedianSortedArrays</span><span class="params">(<span class="keyword">int</span>[] nums1, <span class="keyword">int</span>[] nums2)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (nums1.length &gt; nums2.length)&#123;</span><br><span class="line">        <span class="keyword">return</span> findMedianSortedArrays(nums2, nums1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> len = nums1.length + nums2.length;</span><br><span class="line">    <span class="keyword">int</span> cut1 = <span class="number">0</span>, cut2 = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> cutL = <span class="number">0</span>, cutR = nums1.length;</span><br><span class="line">    <span class="keyword">while</span> (cut1 &lt;= nums1.length)&#123;</span><br><span class="line">        cut1 = (cutR - cutL)/<span class="number">2</span> + cutL;</span><br><span class="line">        cut2 = len / <span class="number">2</span> - cut1;</span><br><span class="line">        <span class="keyword">double</span> L1 = (cut1 == <span class="number">0</span>) ? Integer.MIN_VALUE : nums1[cut1 - <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">double</span> L2 = (cut2 == <span class="number">0</span>) ? Integer.MIN_VALUE : nums2[cut2 - <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">double</span> R1 = (cut1 == nums1.length) ? Integer.MAX_VALUE : nums1[cut1];</span><br><span class="line">        <span class="keyword">double</span> R2 = (cut2 == nums2.length) ? Integer.MAX_VALUE : nums2[cut2];</span><br><span class="line">        <span class="keyword">if</span> (L1 &gt; R2)&#123;</span><br><span class="line">            cutR = cut1 - <span class="number">1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (L2 &gt; R1)&#123;</span><br><span class="line">            cutL = cut1 + <span class="number">1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (len % <span class="number">2</span> == <span class="number">0</span>)&#123;</span><br><span class="line">                L1 = L1 &gt; L2 ? L1 : L2;</span><br><span class="line">                R1 = R1 &lt; R2 ? R1 : R2;</span><br><span class="line">                <span class="keyword">return</span> (L1 + R1) / <span class="number">2</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                R1 = (R1 &lt; R2) ? R1 : R2;</span><br><span class="line">                <span class="keyword">return</span> R1;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LeetCode/" rel="tag"># LeetCode</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/11/06/Java-笔记（一）/" rel="next" title="Java 笔记（一）">
                <i class="fa fa-chevron-left"></i> Java 笔记（一）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/11/14/Java面试总结/" rel="prev" title="Java面试总结">
                Java面试总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">影决</p>
              <p class="site-description motion-element" itemprop="description">在平凡的生活里做着平凡的事，愿我造就不平凡的未来。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#A001-Two-Sum"><span class="nav-number">1.</span> <span class="nav-text">A001 Two Sum</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#A002-Add-Two-Numbers"><span class="nav-number">2.</span> <span class="nav-text">A002 Add Two Numbers</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#A003-Longest-Substring-Without-Repeating-Characters"><span class="nav-number">3.</span> <span class="nav-text">A003 Longest Substring Without Repeating Characters</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#A004-Median-of-Two-Sorted-Arrays"><span class="nav-number">4.</span> <span class="nav-text">A004 Median of Two Sorted Arrays</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">影决</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
</html>
